package com.ywjh.inter.utils;

import java.io.BufferedReader;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

public class StrUtil {

	public static int getInt(String str, int defaultValue) {
		int i = defaultValue;
		try {
			i = Integer.parseInt(str);
		} catch (Exception e) {
		}
		return i;
	}

	public static float getFloat(String str, float defaultValue) {
		float i = defaultValue;
		try {
			i = Float.parseFloat(str);
		} catch (Exception e) {
		}
		return i;
	}

	public static Timestamp getTimestamp(String str) {
		Timestamp t = new Timestamp(System.currentTimeMillis());
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date d = sdf.parse(str);
			t = new Timestamp(d.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return t;
	}

	public static Timestamp getTimestampHHMI(String str) {
		Timestamp t = new Timestamp(System.currentTimeMillis());
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date d = sdf1.parse(str);
			t = new Timestamp(d.getTime());
		} catch (Exception e) {
			System.out.println("error time format:" + str);
		}

		return t;
	}

	public static long getHHMI(String str) {
		long l = 0l;
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date d = sdf1.parse(str);
			l = d.getTime();
		} catch (Exception e) {
			System.out.println("error time format:" + str);
		}

		return l;
	}

	public static Timestamp getTimestampHHMISS(String str) {
		Timestamp t = new Timestamp(System.currentTimeMillis());
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = sdf.parse(str);
			t = new Timestamp(d.getTime());
		} catch (Exception e) {
			System.out.println("error time format:" + str);
		}

		return t;
	}

	public static String getYYYYMMDD(Timestamp t) {
		String d = "0000-00-00";
		if (t == null) {
			return d;
		}
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			d = sdf.format(new Date(t.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	public static String getYYYYMMDD(long t) {
		String d = "0000-00-00";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			d = sdf.format(new Date(t));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	public static String getYYYYMMDDHHMI(Timestamp t) {
		String d = "0000-00-00 00:00";
		if (t == null) {
			return d;
		}
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			d = sdf1.format(new Date(t.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static String getYYYYMMDDHHMISS(Timestamp t) {
		String d = "0000-00-00 00:00:00";
		if (t == null) {
			return d;
		}
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			d = sdf1.format(new Date(t.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	public static String getYYYYMMDDHHMI(long t) {
		String d = "0000-00-00 00:00";
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			d = sdf1.format(new Date(t));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	public static String getPostData(HttpServletRequest req) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = req.getReader();
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public static JSONObject getPostJSON(HttpServletRequest req) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = req.getReader();
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(sb);
	}
}
